package TraitementMessages;

import java.util.List;
import java.util.logging.*;
import mainProcess.MainProcessing;

import ihm.main.Traitement;

import com.lo23.data.objgroup.*;
import com.lo23.data.objinterface.*;

/**
 * @class FriendRequestResponseMessage
 * @author Hicham & Maryam
 */
public class FriendRequestResponseMessage extends FriendMessage {
	
	static final long serialVersionUID = 23201011L;

	 private java.lang.String senderLogin;
	 private boolean agree;
	   
	    public FriendRequestResponseMessage(String senderUID,String senderLogin,boolean agree)
	    {
	    	super(senderUID);
			this.senderLogin = senderLogin;
			this.agree = agree;
	    }
	    
		public void setLogin (String senderLogin) {
	        this.senderLogin = senderLogin;
		}

	    public String getLogin () {
	        return senderLogin;
	    }
	    
		public void setagree (boolean agree) {
			this.agree = agree;
		}

	    public boolean getagree () {
	        return agree;
	    }
	
	
    public void handleReceiption(MainProcessing mainProcessing) {
    	LocalUser lUser = mainProcessing.getDataInterface().getLocalUser(null);

    	if(lUser == null)
    	{
    		Logger.getLogger(FriendRequestResponseMessage.class.getName()).log(Level.SEVERE, "Traitement : Local user Null");
    		return ;
    	}

    	if(agree)
    	{
    		Friend newFriend = lUser.addFriend(senderUID);
    		newFriend.setLogin(senderLogin);
    	}
    	
    	List<FriendshipRequest> listFriend = lUser.getListofSentFriendshipRequest();
        
        for (FriendshipRequest anItem : listFriend) 
        {
        	if (anItem.getFriendID().equals(senderUID) 
        		&& anItem.getStatus()== false) 
        	{
        		anItem.setStatus(true);
        	}
        }
        
        //Envoyer un message pour IHM
        
		Traitement interfaceIHM = Traitement.getInstance();
		interfaceIHM.friendRequestResponse(senderUID);
    }
    
}
